package com.winchain.chainbill.bill.biz.query.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.model.PageModel;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.CommonFileUtil;
import com.winchain.chainbill.bill.base.po.CbCert;
import com.winchain.chainbill.bill.base.service.CbCertService;
import com.winchain.chainbill.bill.biz.query.service.BillQueryService;
import com.winchain.chainbill.bill.biz.query.service.BillTraceService;
import com.winchain.chainbill.bill.biz.query.vo.BillTree;
import com.winchain.chainbill.bill.biz.query.vo.CbRecQueryVo;
import com.winchain.chainbill.bill.biz.query.vo.CbRecVo;
import com.winchain.chainbill.util.ExcelGenUtil;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BillQueryController {
	@Autowired
	BillQueryService billQueryService;
	@Autowired
	BillTraceService billTraceService;
	@Autowired
	private CbCertService cbCertService;

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������������")
	public List<CbRecVo> selectOwnBillByPage(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		return this.billQueryService.selectOwnBillByPage(limit, cbRecordQueryVo);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public List<CbRecVo> queryBills(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		List<CbRecVo> list = this.billQueryService.queryBills(limit, cbRecordQueryVo, "0");
		PageModel<CbRecVo> pageModel = (PageModel) list;
		pageModel.setExtend(this.billQueryService.queryBillStats(cbRecordQueryVo, "0"));
		return list;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public void exportQueryBills(PageLimit limit, CbRecQueryVo cbRecordQueryVo, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap();
		map.put("datas", this.billQueryService.queryBills(limit, cbRecordQueryVo, "0"));

		String execelTemp = "";
		String fileName = "��������";
		switch (cbRecordQueryVo.getTxType()) {
		case "0":
			execelTemp = "/template/excel/querySendBills.xlsx";
			fileName = "��������";
		case "10":
			execelTemp = "/template/excel/querySendBills.xlsx";
			fileName = "����������";
			break;
		case "8":
			execelTemp = "/template/excel/querySendBills.xlsx";
			fileName = "����������";
			break;
		case "1":
			execelTemp = "/template/excel/queryTransferBills.xlsx";
			fileName = "��������";
			break;
		case "11":
			execelTemp = "/template/excel/queryTransferBills.xlsx";
			fileName = "������";
			break;
		case "2":
			execelTemp = "/template/excel/queryDiscountBills.xlsx";
			fileName = "��������";
			break;
		case "12":
			execelTemp = "/template/excel/queryDiscountBills.xlsx";
			fileName = "����������";
			break;
		case "3":
			execelTemp = "/template/excel/queryReceiveBills.xlsx";
			fileName = "��������";
			break;
		case "13":
			execelTemp = "/template/excel/queryReceiveBills.xlsx";
			fileName = "������";
			break;
		case "9":
			execelTemp = "/template/excel/queryReceiveBills.xlsx";
			fileName = "������";
			break;
		case "7":
			execelTemp = "/template/excel/queryReceiveBills.xlsx";
			fileName = "������";
			break;
		}
		map.put("listNm", fileName);
		InputStream is = ExcelGenUtil.getInstance().GenerateExcelForStream(execelTemp, map);
		CommonFileUtil.writeFileToClient(fileName + ".xlsx", is, response, request);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������")
	public List<CbRecVo> queryBillsForMgt(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		List<CbRecVo> list = this.billQueryService.queryBills(limit, cbRecordQueryVo, "1");
		PageModel<CbRecVo> pageModel = (PageModel) list;
		pageModel.setExtend(this.billQueryService.queryBillStats(cbRecordQueryVo, "1"));
		return list;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������")
	public List<CbRecVo> queryBillsForAdmin(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		List<CbRecVo> list = this.billQueryService.queryBills(limit, cbRecordQueryVo, "");
		PageModel<CbRecVo> pageModel = (PageModel) list;
		pageModel.setExtend(this.billQueryService.queryBillStats(cbRecordQueryVo, ""));
		return list;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������")
	public void exportBillsForMgt(PageLimit limit, CbRecQueryVo cbRecordQueryVo, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap();
		map.put("datas", this.billQueryService.queryBills(limit, cbRecordQueryVo, "1"));
		String execelTemp = "";
		String fileName = "";
		String txType = cbRecordQueryVo.getTxType();
		if (txType.equals("all")) {
			execelTemp = "/template/excel/queryMgtAllBills.xlsx";
			fileName = "����.xlsx";
		} else if (txType.equals("0")) {
			execelTemp = "/template/excel/queryMgtSendBills.xlsx";
			fileName = "����.xlsx";
		} else if (txType.equals("1")) {
			execelTemp = "/template/excel/queryMgtTransferBills.xlsx";
			fileName = "����.xlsx";
		} else if (txType.equals("2")) {
			execelTemp = "/template/excel/queryMgtDiscountBills.xlsx";
			fileName = "����.xlsx";
		} else if (txType.equals("3")) {
			execelTemp = "/template/excel/queryMgtReceiveBills.xlsx";
			fileName = "����.xlsx";
		} else if (txType.equals("6")) {
			execelTemp = "/template/excel/queryMgtDeferralBills.xlsx";
			fileName = "����.xlsx";
		} else if (txType.equals("4")) {
			execelTemp = "/template/excel/queryMgtRepurchaseBills.xlsx";
			fileName = "����.xlsx";
		}
		InputStream is = ExcelGenUtil.getInstance().GenerateExcelForStream(execelTemp, map);
		CommonFileUtil.writeFileToClient(fileName, is, response, request);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public CbRecVo queryBillDetail(String billId, String level, String txType, String bizId) {
		return this.billQueryService.queryBillDetail(billId, txType, bizId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public BillTree queryBillOrbit(String billId, int level) {
		if (level >= 0) {
			return this.billTraceService.concise(billId, level);
		}
		return this.billTraceService.total(billId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public Map<String, Object> queryBillTotalStats() {
		return this.billQueryService.queryBillTotalStats();
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public List<CbRecVo> queryBillDetailForCert(String billIds, String billTypes, String bizIds) {
		List<CbRecVo> list = new ArrayList();
		String[] ids = billIds.split(",");
		String[] types = billTypes.split(",");
		String[] bizs = bizIds.split(",");
		for (int i = 0; i < ids.length; i++) {
			CbRecVo cbRecVo = this.billQueryService.queryBillDetail(ids[i], types[i], bizs[i]);
			String[] bizId = null;
			if ((!StringUtil.emptyAndNull(billTypes)) && (("12".equals(billTypes)) || ("11".equals(billTypes))
					|| ("13".equals(billTypes)) || ("10".equals(billTypes)))) {
				bizId = new String[] { bizs[i] };
			}
			List<CbCert> cbCerts = this.cbCertService.selectCertByBillIds(new String[] { ids[i] }, bizId);
			if ((cbCerts != null) && (cbCerts.size() > 0)) {
				cbRecVo.setRcvDate(((CbCert) cbCerts.get(0)).getCertDate());
			}
			cbRecVo.setTxType(types[i]);
			list.add(cbRecVo);
		}
		return list;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public BigDecimal getTxTotal() {
		return this.billQueryService.getTxTotal(CommonDataUtil.getCurEntId());
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������")
	public int selectBillCount(String txType) {
		return this.billQueryService.selectBillCount(txType);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public Map<String, Object> queryBillStats(String txType) {
		CbRecQueryVo cbRecordQueryVo = new CbRecQueryVo();
		cbRecordQueryVo.setTxType(txType);
		Map<String, Object> m = this.billQueryService.queryBillStats(cbRecordQueryVo, "0");
		return m;
	}
}
